package com.tourex.ui;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.android.volley.Response;
import com.google.gson.reflect.TypeToken;
import com.tourex.BaseActivity;
import com.tourex.C;
import com.tourex.C.bean.TourProjectInfo;
import com.tourex.R;
import com.tourex.thirdparty.view.tabstrip.PagerSlidingTabStrip;
import com.tourex.thirdparty.volley.VolleyUtils;
import com.tourex.ui.fragment.Second1Fragment;
import com.tourex.ui.fragment.Second2Fragment;
import com.tourex.utils.BaseUtils;

public class SecondActivity extends BaseActivity {

	private Second1Fragment pickFragment;
	private Second1Fragment cateFragment;
	private Second1Fragment babyFragment;
	private Second2Fragment shoppingFragment;
	private Second1Fragment xianluFragment;
	private Second1Fragment yizhanFragment;

	private PagerSlidingTabStrip tabs;

	public String cityCode;

	public static final String E_WHICH = "e_which";
	public static final String E_CITYCODE = "e_cityCode";

	public static void actionStart(Context context, int which, String cityCode) {
		Intent intent = new Intent(context, SecondActivity.class);
		intent.putExtra(E_WHICH, which);
		intent.putExtra(E_CITYCODE, cityCode);
		context.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_second);
		super.onCreate(savedInstanceState);
		cityCode = getIntent().getStringExtra(cityCode);

		titleLayout.addLeftDrawableMenu(this, R.drawable.ico_back, 25, 25, onBackClickListener);
		// titleLayout.addRightDrawableMenu(this, R.drawable.ico_star_solid, 25,
		// 25, onToMainClickListener);

		titleLayout.setTitle(R.string.app_name);

		babyFragment = Second1Fragment.newInstance(C.convention.type_qz);
		pickFragment = Second1Fragment.newInstance(C.convention.type_cz);
		cateFragment = Second1Fragment.newInstance(C.convention.type_ms);
		shoppingFragment = Second2Fragment.newInstance(C.convention.type_tg);
		xianluFragment = Second1Fragment.newInstance(C.convention.type_xl);
		yizhanFragment = Second1Fragment.newInstance(C.convention.type_njl);

		ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
		tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
		pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
		tabs.setViewPager(pager);
		setTabsValue();

		int which = getIntent().getIntExtra(E_WHICH, 0);
		pager.setCurrentItem(which);
	}

	public void requestApi02(final String type, String cityCode) {
		// http://222.85.150.217:8099/appServer/gzly/server!loadI02.do?type=ms&city=488
		Map<String, String> params = BaseUtils.initMap("type", type, "city", cityCode);
		VolleyUtils.postString(C.api.api02, params, new Response.Listener<String>() {
			@Override
			public void onResponse(String response) {
				List<TourProjectInfo> TourProjectInfoList = gson.fromJson(response, new TypeToken<List<TourProjectInfo>>() {
				}.getType());

				if (type.equals(C.convention.type_ms)) {
					cateFragment.setData(TourProjectInfoList);
				} else if (type.equals(C.convention.type_cz)) {
					pickFragment.setData(TourProjectInfoList);
				} else if (type.equals(C.convention.type_qz)) {
					babyFragment.setData(TourProjectInfoList);
				} else if (type.equals(C.convention.type_tg)) {
					shoppingFragment.setData(TourProjectInfoList);
				} else if (type.equals(C.convention.type_xl)) {
					xianluFragment.setData(TourProjectInfoList);
				} else if (type.equals(C.convention.type_njl)) {
					yizhanFragment.setData(TourProjectInfoList);
				}
			}
		});

	}

	private void setTabsValue() {
		tabs.setShouldExpand(true);
		tabs.setDividerColor(Color.TRANSPARENT);
		tabs.setUnderlineHeight(BaseUtils.dip(this, 1));
		tabs.setIndicatorHeight(BaseUtils.dip(this, 4));
		tabs.setTextSize(BaseUtils.sp(this, 16));
		tabs.setIndicatorColor(Color.parseColor("#4ABDCC"));
		tabs.setSelectedTextColor(Color.parseColor("#4ABDCC"));
		tabs.setTabBackground(0);
	}

	public class MyPagerAdapter extends FragmentPagerAdapter {

		private final String[] titles;

		public MyPagerAdapter(FragmentManager fm) {
			super(fm);
			titles = getResources().getStringArray(R.array.second_titles);
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return titles[position];
		}

		@Override
		public int getCount() {
			return titles.length;
		}

		@Override
		public Fragment getItem(int position) {
			switch (position) {
			case 0:
				return cateFragment;
			case 1:
				return pickFragment;
			case 2:
				return babyFragment;
			case 3:
				return shoppingFragment;
			case 4:
				return xianluFragment;
			case 5:
				return yizhanFragment;

			default:
				return null;
			}
		}
	}

}
